package com.example.index.web;

import com.example.user.bean.Result;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MQConsume {

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub1", durable = "true"),
            exchange = @Exchange(value="fanoutTest", type = ExchangeTypes.FANOUT)
    )})
    public void recvFanoutMessage(Result result){
        System.out.println("fanout-1 : " + result);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub2", durable = "true"),
            exchange = @Exchange(value="fanoutTest", type = ExchangeTypes.FANOUT)
    )})
    public void recvFanoutMessage1(Result result){
        System.out.println("fanout-2 : " + result);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="fanoutTest", type = ExchangeTypes.FANOUT)
    )})
    public void recvFanoutMessage3(Result result){
        System.out.println("fanout-3 : " + result);
    }

/////////////////////////////////// 定向路由 DIRECT ////////////////////////////////
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub3", durable = "true"),
            exchange = @Exchange(value="directTest", type = ExchangeTypes.DIRECT),
            key = {"order"}
    )})
    public void recvDirectMessage(Result result){
        System.out.println("direct-1 : " + result);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub4", durable = "true"),
            exchange = @Exchange(value="directTest", type = ExchangeTypes.DIRECT),
            key = {"page"}
    )})
    public void recvDirectMessage1(Result result){
        System.out.println("direct-2 : " + result);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="directTest", type = ExchangeTypes.DIRECT),
            key = {"order","page"}
    )})
    public void recvDirectMessage3(Result result){
        System.out.println("direct-3 : " + result);
    }

    /////////////////////////////////// 主题路由 TOPIC ////////////////////////////////
    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub5", durable = "true"),
            exchange = @Exchange(value="topicTest", type = ExchangeTypes.TOPIC),
            key = {"#.order.*","*.cart.#"}
    )})
    public void recvTopicMessage(Result result){
        System.out.println("direct-1 : " + result);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "pubSub6", durable = "true"),
            exchange = @Exchange(value="topicTest", type = ExchangeTypes.TOPIC),
            key = {"user.*"}
    )})
    public void recvTopicMessage1(Result result){
        System.out.println("direct-2 : " + result);
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="topicTest", type = ExchangeTypes.TOPIC),
            key = {"dept.#"}
    )})
    public void recvTopicMessage3(Result result){
        System.out.println("direct-3 : " + result);
    }

}
